package com.rjkj.medicalwaste.viewmodels.provider

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.rjkj.medicalwaste.database.MainDatabase
import com.rjkj.medicalwaste.database.getDatabase
import com.rjkj.medicalwaste.repository.BoxRepository
import com.rjkj.medicalwaste.repository.GarbageRepository
import com.rjkj.medicalwaste.repository.UserRepository
import com.rjkj.medicalwaste.ui.xlb.AllUserDataViewModel
import com.rjkj.medicalwaste.ui.xlb.XlbTestViewModel
import com.rjkj.medicalwaste.viewmodels.*

object MainViewModelProvider {
    fun provideMainViewModel(context: Context): MainViewModelFactory {
        return MainViewModelFactory(getDatabase(context))
    }
    fun provideUserModel(context: Context): UserModelFactory {
        return UserModelFactory(UserRepository.getInstance(getDatabase(context).userDao))
    }
    fun provideUnBoxedModel(context: Context): UnBoxedModelFactory {
        val database = getDatabase(context)
        return UnBoxedModelFactory(
                GarbageRepository.getInstance(database.garbageDao)
        )
    }

    fun provideOutboundViewModel(context: Context): OutboundViewModelFactory {
        val database = getDatabase(context)
        return OutboundViewModelFactory(
                GarbageRepository.getInstance(database.garbageDao)
        )
    }

    fun providerXlbTestModel(context: Context): XlbTestModelFactory {
        val repository: UserRepository = UserRepository.getInstance(getDatabase(context).userDao)
        val repository2: GarbageRepository = GarbageRepository.getInstance(getDatabase(context).garbageDao)
        return XlbTestModelFactory(repository,repository2)
    }

    fun providerAllUserModel(context: Context): ShowAllUserDataFactory {
        val repository: UserRepository = UserRepository.getInstance(getDatabase(context).userDao)
        return ShowAllUserDataFactory(repository)
    }


    //CollectOneViewModel
    fun providerCollectOneModel(context: Context): CollectOneModelFactory {
        return CollectOneModelFactory(GarbageRepository.getInstance(getDatabase(context).garbageDao))
    }
    fun providerReviewWeightModel(context: Context,code: String): ReviewWeightModelFactory {
        return ReviewWeightModelFactory(GarbageRepository.getInstance(getDatabase(context).garbageDao),code)
    }
    //CollectOneViewModel
    fun providerCollectMainModel(context: Context): CollectMainModelFactory {
        val database = getDatabase(context)
        return CollectMainModelFactory(
                GarbageRepository.getInstance(database.garbageDao),
        )
    }
}
class MainViewModelFactory(
    private val database: MainDatabase,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return MainViewModel(database) as T
    }
}
class UserModelFactory(
        private val userRepository: UserRepository,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return UserViewModel(userRepository) as T
    }
}
class UnBoxedModelFactory(
        private val repository: GarbageRepository,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return UnBoxedViewModel(repository) as T
    }
}
class OutboundViewModelFactory(
        private val repository: GarbageRepository,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return OutboundViewModel(repository) as T
    }
}


class CollectMainModelFactory(
        private val repository: GarbageRepository,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return CollectMainViewModel(repository) as T
    }
}
class ReviewWeightModelFactory(
        private val repository: GarbageRepository,
        private val code:String
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return ReviewWeightViewModel(repository,code) as T
    }
}
class CollectOneModelFactory(
        private val repository: GarbageRepository,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return CollectOneViewModel(repository) as T
    }
}

class XlbTestModelFactory(
        private val repository: UserRepository,
        private val repository2: GarbageRepository,
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return XlbTestViewModel(repository,repository2) as T
    }
}

class ShowAllUserDataFactory(
        private val repository: UserRepository
) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return AllUserDataViewModel(repository) as T
    }
}

